package org.qiyu.live.account.provider.service.Impl;

import jakarta.annotation.Resource;
import org.idea.qiyu.live.framework.redis.starter.key.AccountProviderCacheKeyBulider;
import org.qiyu.live.account.provider.service.IAccountTockenService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Description: //todo
 * @Author: 张广焱
 * @Date: 2024-04-27 上午2:32
 */
@Service
public class AccountTockenServiceImpl implements IAccountTockenService {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private AccountProviderCacheKeyBulider cacheKeyBulider;

    @Override
    public String createAndSaveLoginToken(Long userId) {
        //生成token
        String token = UUID.randomUUID().toString();
        redisTemplate.opsForValue().set(cacheKeyBulider.buildUserLoginToken(token), userId.toString(), 30, TimeUnit.DAYS);
        return token;
    }

    @Override
    public Long getUserIdByToken(String tokenKey) {
        String redisKey = cacheKeyBulider.buildUserLoginToken(tokenKey);
        Integer userId = (Integer) redisTemplate.opsForValue().get(redisKey);
        return userId == null ? null : Long.valueOf(userId);
    }
}
